Modifying Recorded Scripts
You can often modify a recorded script to suit new circumstances. For example, you could adapt the script shown in Listing 1-1 for use with an external hard disk rather than the startup disk by changing the termstartup disk
todisk
"nameOfDisk" throughout the script, where nameOfDisk is the name of the hard disk on which the folders Financial and Letters are located.Or suppose you want to make sure the Finder opens each window specified in Listing 1-1 with a specific view selected in the Views menu. One way to do this might be to place the insertion point after the last line of the script in the script window, turn on recording, choose the settings you want for each window from the View menu, and turn off recording. Any significant changes you make to the windows' views will be recorded; however, if some of the View settings for some of the windows are already set the way you want them, the Finder won't record the action because nothing changes as a result.
A more reliable approach is to add lines to the recorded script that set the View settings. To do so, you must know how to tell the Finder to set the view of
a window.The easiest way to learn how to describe an action in the Finder is to try recording the action in a sample script. To learn how the Finder sets a window's view, follow these steps:
The recorded script looks something like this:
- Open a new script window in the Script Editor by choosing New Script from the File menu.
- Click the Record button.
- Activate the Finder by clicking on the desktop or choosing Finder from the Applications menu.
- Change the Views setting for one of the Finder's windows.
- Activate the Script Editor again and click the Stop button.
tell application "Finder" activate set view of window of folder "Financial" of startup disk to name end tellThe third line of the recorded script sets the View property of the window for the folder Financial toname
. You can now use this line as a template for setting the views of folder windows in a script. For example, to modify Listing 1-1 so that the windows will always open in specific views, you could add these lines just before the last line (that is, beforeend tell
):
set view of window of startup disk to icon set view of window of folder "Financial" of startup disk to name set view of window of folder "Letters" of startup disk to sizeAfter you run the new script, the windows have the views specified in the script even if you've previously changed them.To modify recorded scripts or create new scripts that control the Finder, you may need to look up the definitions of some of Finder terms. Like any scriptable application, the Finder contains a dictionary of the AppleScript terms that you can use to control the application. To open the dictionary, drag the Finder's icon over the icon of the Script Editor, the script-editing application that comes with AppleScript. If you run across an unfamiliar word in a recorded script, you can look it up quickly in the dictionary. If you need a comprehensive definition with examples, look up the term's entry in this book.